package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.SysParamter;
import com.ruoyi.system.service.ISysParamterService;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.service.ISysFloorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 楼层管理Controller
 */
@Controller
@RequestMapping("/sujian/floorManagement")
public class FloorManController extends BaseController
{
    private String prefix = "propertys/sujian/floorManagement";

    @Autowired
    private ISysFloorService sysFloorService;

    @Autowired
    private ISysParamterService sysParamterService;

    @GetMapping()
    public String floorMan(ModelMap modelMap)
    {
        List<SysFloor> sysFloorList = sysFloorService.selectSysFloorListAll(new SysFloor());
        modelMap.put("sysFloorList",sysFloorList);
        return prefix + "/floor";
    }


    /**
     * 查询楼层管理信息列表
     */
//    @RequiresPermissions("staff:floorManagement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFloor SysFloor)
    {
        startPage();
        //获取当前用户下所有项目id集合
//        List<String> projectIds = xxxxx(ShiroUtils.getUserId());
//        SysFloor.setProjectIds(projectIds);
        List<SysFloor> list = sysFloorService.selectSysFloorList(SysFloor);
        return getDataTable(list);
    }

    /**
     * 新增楼层管理信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysFloor SysFloor = new SysFloor();
        List<SysFloor> SysFloorList = sysFloorService.selectSysFloorList(SysFloor);
        mmap.put("SysFloorList", SysFloorList);
        List<SysParamter> sysParamterList = sysParamterService.listSysParamterAll();
        mmap.put("sysParamterList",sysParamterList);
        return prefix + "/add";
    }

    /**
     * 新增保存楼层管理信息
     */
//    @RequiresPermissions("sujian:floorManagement:add")
    @Log(title = "楼层管理信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysFloor SysFloor)
    {
        SysFloor.setFloorId(UUID.randomUUID().toString());
        SysFloor.setUpdatetime(new Date());
        if(sysFloorService.checkfloorNameExist(SysFloor.getFloorName())){
            return AjaxResult.error("该楼层已经存在，请勿重复添加!");
        }else {
            return toAjax(sysFloorService.insertSysFloor(SysFloor));
        }
    }

    /**
     * 修改楼层管理信息
     */
    @GetMapping("/edit/{floorId}")
    public String edit(@PathVariable("floorId") String floorId, ModelMap mmap)
    {
        System.out.println(floorId);
        SysFloor SysFloor = sysFloorService.selectSysFloorById(floorId);
        mmap.put("SysFloor", SysFloor);
        List<SysParamter> sysParamterList = sysParamterService.listSysParamterAll();
        mmap.put("sysParamterList",sysParamterList);
        return prefix + "/edit";
    }

    /**
     * 修改楼层管理基础信息
     */
//    @RequiresPermissions("sujian:floorManagement:edit")
    @Log(title = "楼层管理信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysFloor SysFloor)
    {
        SysFloor.setUpdatetime(new Date());
        return toAjax(sysFloorService.updateSysFloor(SysFloor));
    }

    /**
     * 删除楼层管理信息
     */
//    @RequiresPermissions("sujian:floorManagement:remove")
    @Log(title = "楼层管理信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids)
    {
        return toAjax(sysFloorService.deleteFloornfoByIds(ids));
    }

    /**
     * 导入楼层管理列表
     * */
    @Log(title = "楼层管理", businessType = BusinessType.IMPORT)
//    @RequiresPermissions("sujian:floorManagement:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importExcel(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysFloor> util = new ExcelUtil<>(SysFloor.class);
        List<SysFloor> lists = util.importExcel(file.getInputStream());
        SysUser sysUser = ShiroUtils.getSysUser();
        //校验数据
        String res = sysFloorService.validateDatas(lists);
        if (StringUtils.isNotBlank(res)){
            return AjaxResult.error(res);
        }
        String message =sysFloorService.importSysFloor(lists,updateSupport,sysUser);
        return AjaxResult.success(message);
    }


    /**
     * 导入中的下载模板
     * */
//    @RequiresPermissions("sujian:floorManagement:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysFloor> util = new ExcelUtil<SysFloor>(SysFloor.class);
        return util.importTemplateExcel("楼层管理信息");
    }


    /**
     * 导出楼层管理信息列表
     */
//    @RequiresPermissions("sujian:floorManagement:export")
    @Log(title = "楼层管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String ids)
    {
        String[] floorIds = Convert.toStrArray(ids);
        List<SysFloor> list = sysFloorService.selectSysFloorListByIds(floorIds);
        ExcelUtil<SysFloor> util = new ExcelUtil<SysFloor>(SysFloor.class);
        return util.exportExcel(list, "楼层信息");
    }



}
